import {
  request
} from "../../utils/request";
import {
  realNameAuth
} from "../../api/custBaseController";
import {
  getCustBaseByUserLoginState
} from "../../api/custBaseController";
import {
  BASE_API
} from "../../utils/config";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    formList: [{
        name: '姓名',
        icon: 'iconxingming',
        value: '',
        maxlength: '-1'
      },
      {
        name: '身份证号',
        icon: 'iconshenfenzhenghao',
        value: '',
        maxlength: '18'
      },
      {
        name: '图片验证码',
        icon: 'icontupian',
        value: '',
        maxlength: '4'
      },
    ],
    userLoginState: '',
    random: Math.random()
  },
  onShow() {
    this.setData({
      api: BASE_API,
      userLoginState: wx.getStorageSync('userLoginState')
    })
  },
  /**
   * 保存表单内容
   */
  getInputValue(e) {
    let {
      index
    } = e.currentTarget.dataset
    let {
      value
    } = e.detail
    this.setData({
      ['formList[' + index + '].value']: value
    })
  },
  changeRandomTime() {
    this.setData({
      random: Math.random()
    })
  },
  /**
   * 实名认证
   */
  async realNameAuth() {
    let self = this
    let temp = self.dataVerify()
    if (!temp) {
      return
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    /**
     * 提示信息 一闪而过 ———— 修复
     */
    wx.request({
      url: self.data.api + '/custBaseController/realNameAuth' + '?userLoginState=' + wx.getStorageSync('userLoginState'),
      data: {
        name: self.data.formList[0].value,
        idCard: self.data.formList[1].value,
        imageCode: self.data.formList[2].value,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: (res) => {
        wx.hideLoading()
        if (res.data.success) {
          wx.showToast({
            title: '认证成功',
            mask: true,
          })
          setTimeout(() => {
            self.getCustBaseByUserLoginState()
          }, 1500);
        } else {
          wx.showToast({
            title: res.data.errorMessage,
            mask: true,
            duration: 2000,
            icon: 'none'
          })
          setTimeout(() => {
            self.setData({
              random: Math.random()
            })
          }, 2000);
        }
      },
      fail: (res) => {
        wx.hideLoading()
        wx.showToast({
          title: '认证失败！',
          icon: 'none',
          mask: true,
        })
      },
    })
    // let resolve = await realNameAuth({
    //   name: this.data.formList[0].value,
    //   idCard: this.data.formList[1].value,
    //   imageCode: this.data.formList[2].value,
    // })
    // if (resolve.success) {
    //   wx.showToast({
    //     title: '认证成功',
    //     mask: true,
    //   })
    //   setTimeout(() => {
    //     this.getCustBaseByUserLoginState()
    //   }, 1500);
    // }else{
    //   this.setData({
    //     random: Math.random()
    //   })
    // }
  },
  /**
   * 根据userLoginState获取当前客户
   */
  async getCustBaseByUserLoginState() {
    let resolve = await getCustBaseByUserLoginState()
    if (resolve.success) {
      wx.setStorageSync('userBase', resolve.data)
      wx.navigateBack({
        delta: 1,
      })
    }
  },
  /**
   * 表单验证
   */
  dataVerify() {
    var a = this.data.formList[0].value
    var b = this.data.formList[1].value
    var certCodeReg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([012]\d)|(3[0-1]))\d{3}([0-9]|X)$/; //身份证号正则

    if (a == null || a == '') {
      wx.showToast({
        icon: 'none',
        mask: false,
        duration: 1500,
        title: '姓名不能为空'
      })
      return false
    }
    if (b == null || b == '') {
      wx.showToast({
        icon: 'none',
        mask: false,
        duration: 1500,
        title: '身份证号码不能为空'
      })
      return false
    }
    if (!certCodeReg.test(b)) {
      wx.showToast({
        icon: 'none',
        mask: false,
        duration: 1500,
        title: '请输入正确的身份证号码'
      })
      return false
    }
    return true
  }
})